$(function () {
    var _myScroll;

    var messagePage = function () {
        this.init();
    };
    messagePage.prototype = {
        // 初始化页面
        init: function () {
            this.loadData();
            this.bindEvent();
        },
        // 加载数据
        loadData: function () {
            var model = this;
            // 3.新闻播报 4.促销活动
            var postdata = {index: 1, size: 20, search: {type: "3|4"}, sorting: {time: "DESC"}};
            $.ajax({
                headers: {'Content-Type': 'application/json'},
                type: "POST",
                url: CONFIG_MANAGER.RequstUrl + "v1/news/lists",
                data: JSON.stringify(postdata)
            }).
            done(function (res) {
                var newslist = JSON.parse(res).data;
                model.paintPage(newslist);
                // 最后查看日期
                setLocalStorage(STORAGE_MANAGER.lastCheckTime, new Date().getTime());
            });
        },

        // 画页面
        paintPage: function (newslist) {
            var html = [];
            var itemwidth = $(window).width() * 0.8;
            if (!newslist || newslist.length == 0) {
                $("#newslistdiv").html("<div style='padding-top: 40%;text-align: center'>暂无数据</div>");
                return;
            }
            $.each(newslist, function (i, item) {
                var create_time = new Date(toDateString(item.create_time, "yyyy-MM-dd hh:mm:ss")).getTime();
                var content = $("<label></label>").html(item.content);
                var isdisplayMore = (content.width() > itemwidth) ? "" : "style='display:none '";
                if (item.type == 4) {
                    html.push("<div class='xxggbg mbbottom'><div class='xgtb'><img src='../../images/ggtb2.png' /></div><div class='xgny'>");
                    html.push("<p class='xgnybt'><span>" + dateDiff(create_time) + "</span>促销活动</p>");

                    html.push("<p class='xgnynr'><b><label>" + item.content + "</label><span name='btnshow' " + isdisplayMore + "><a>展开详情</a></span></b></p>");
                    html.push("</div></div>");
                } else {
                    html.push("<div class='xxggbg mbbottom'><div class='xgtb'><img src='../../images/ggtb1.png' /></div><div class='xgny'>");
                    html.push("<p class='xgnybt'><span>" + dateDiff(create_time) + "</span>新闻播报</p>");
                    html.push("<p class='xgnynr'><b>" + item.content + "</b> <span name='btnshow' " + isdisplayMore + "><a>展开详情</a></span></p>");
                    html.push("</div></div>");
                }
            });

            $("#newslistdiv").html(html.join(""));
        },

        // 绑定事件
        bindEvent: function () {
            // 置顶
            $("#btnback").on("tap", function () {
                back();
            });

            // 展开/收起
            $("#newslistdiv").on("tap", "span[name=btnshow] a", function () {
                var className = "";
                if ($(this).text() == "展开详情") {
                    // 展开
                    $(this).parents("p")[0].className = "zhankai";
                    $(this).text("收起详情");
                } else {
                    // 收起
                    $(this).parents("p")[0].className = "xgnynr";
                    $(this).text("展开详情");
                }
            });
        },
    };

    var _messagePage = new messagePage();
});
